package br.ufrpe.bsi.pvs.xml;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;

import br.ufrpe.bsi.pvs.ResponseQueue;

/** class made for utilities with the xml system of PVS */
public abstract class XMLUtil {

	// <files>
	public static final String REQUEST_FILE = "/process/request.xml";

	public static final String RESPONSE_FILE = "/process/response.xml";
	// </files>

	// <main-elements>
	public static final String ELEM_ROOT = "pvs";

	public static final String ELEM_NULL = "null";

	public static final String ELEM_REQUEST = "request";

	public static final String ELEM_RESPONSE = "response";

	public static final String ELEM_CLASS = "class";

	public static final String ELEM_MSG = "message";

	public static final String ATTR_NAME = "name";
	// </main-elements>

	// <requests>
	public static final String ADD = "add";

	public static final String LIST = "list";

	public static final String QUERY = "query";

	public static final String LOGIN = "login";
	// </requests>

	// <responses>
	public static final String ADDED = "added";

	public static final String LISTED = "listed";

	public static final String QUERIED = "queried";

	public static final String LOGGED = "logged";

	// </responses>

	/** generates a defaul null document for erase request/response messages */
	public static Document getNullRequestDocument() {
		Document document = new Document();

		Element root = new Element(XMLUtil.ELEM_NULL);
		document.setRootElement(root);

		return document;
	}

	/** writes on request.xml */
	public static void writeRequest(Document docF) {
		writeInXML(docF, XMLUtil.REQUEST_FILE,
				"[XML OUTPUT ERROR] cant write request: ");
	}

	/** writes on request.xml */
	public static void writeResponse(Document docF) {
		writeInXML(docF, XMLUtil.RESPONSE_FILE,
				"[XML OUTPUT ERROR] cant write response: ");
	}

	/** effective write on xml file */
	private static void writeInXML(Document doc, String fileName,
			String errorMsg) {
		URL url = XMLUtil.class.getResource(fileName);
		try (OutputStream out = new BufferedOutputStream(new FileOutputStream(
				new File(url.toURI())))) {
			XMLOutputter xout = new XMLOutputter();
			xout.output(doc, out);
		} catch (Exception e) {
			System.err.println(errorMsg + e);
		}
	}

	/** read response.xml */
	public static Object readResponse(Document docF) {
		// extrai o tipo de request que foi feito
		Element root = docF.getRootElement();
		Element request = root.getChild(XMLUtil.ELEM_REQUEST);
		Attribute name = request.getAttribute(XMLUtil.ATTR_NAME);
		String value = name.getValue();

		// obtem a resposta esperada
		String responseIndex = "";
		switch (value) {
		case XMLUtil.ADD:
			responseIndex = XMLUtil.ADDED;
			break;
		case XMLUtil.LIST:
			responseIndex = XMLUtil.LISTED;
			break;
		case XMLUtil.QUERY:
			responseIndex = XMLUtil.QUERIED;
			break;
		case XMLUtil.LOGIN:
			responseIndex = XMLUtil.LOGGED;
			break;
		}

		// retorna da fila de respostas o resultado
		return ResponseQueue.INSTANCE.getResponse(responseIndex);
	}
}
